/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.TreeMap;
import units.CellType;
import units.MonkeyType;
import utils.FileUtils;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class MakeClustersTable_together {

    private double membershipCutoff = 0.6;
    private int otherMemberId = 0;
    private int maxNoOfClusters = 15;
    private boolean useCellTypeL = true;
    private File clustersFolder = new File("/Users/jamie/Dropbox/monkeys/clust/clusters");
    private TreeMap<String, Double> bestCoefficients;
    private TreeMap<String, String> membership;
    private boolean isgsOnly = true;

    public void fillTables() {

        Handle handle = Handle.getConnectedHandle("monkeys");
        String cells;
        if (useCellTypeL) {
            cells = "PL";
        } else {
            cells = "P";
        }


        bestCoefficients = new TreeMap<String, Double>();
        membership = new TreeMap<String, String>();
        for (int i = 1; i <= maxNoOfClusters; i++) {
            String fileName = "allTogether_"+cells+"_POST_" + i + ".txt";
            if(isgsOnly){
                fileName = "allTogether_"+cells+"_POST_isgsOnly_" + i + ".txt";
            }
            File f = new File(clustersFolder, fileName);
            if (f.exists()) {
                process(handle, f, i);
            }
        }
        processMembers(handle, bestCoefficients, membership);
        handle.closeConnection();
    }

    private void process(Handle handle, File f, int clusterNo) {
        BufferedReader reader = FileUtils.getBufferedReader(f);
        String line;
        String clusterId = clusterNo+"";
        try {
            while ((line = reader.readLine()) != null) {
                String[] split = line.split("\t");
                String probeId = split[0];
                double coeff = Double.parseDouble(split[1]);
                if (!bestCoefficients.containsKey(probeId)) {
                    if (coeff > membershipCutoff) {
                        bestCoefficients.put(probeId, coeff);
                        membership.put(probeId, clusterId);
                    } else {
                        bestCoefficients.put(probeId, coeff);
                        membership.put(probeId, ""+otherMemberId);
                    }
                } else {
                    if (bestCoefficients.get(probeId) < coeff) {
                        if (coeff > membershipCutoff) {
                            bestCoefficients.put(probeId, coeff);
                            membership.put(probeId, clusterId);
                        } else {
                            bestCoefficients.put(probeId, coeff);
                            membership.put(probeId, ""+otherMemberId);
                        }
                    }
                }
                if(!isgsOnly){
                    insert(handle, "clusterData", clusterId, probeId, coeff);
                } else {
                    insert(handle, "clusterData_isgsOnly", clusterId, probeId, coeff);
                }
            }
            reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void makeClusterDataTable() {
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql = "drop table if exists clusterData";
        handle.runSQLupdate(sql);
        sql = "create table clusterData (id int(10) primary key auto_increment, clusterId int(5) not null, probeId int(12) not null, coefficient double not null, monkeyType char(1) not null, cellType char(1) not null)";
        handle.runSQLupdate(sql);
        sql = "create index clusterId_index on clusterData(clusterId)";
        handle.runSQLupdate(sql);
        sql = "create index probeId_index on clusterData(probeId)";
        handle.runSQLupdate(sql);
        handle.closeConnection();
    }

    public void makeClusterMembershipTable() {
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql = "drop table if exists clusterMembership";
        handle.runSQLupdate(sql);
        sql = "create table clusterMembership (id int(10) primary key auto_increment, clusterId int(5) not null, probeId int(12) not null, coefficient double not null, monkeyType char(1) not null, cellType char(1) not null)";
        handle.runSQLupdate(sql);
        sql = "create index clusterId_index on clusterMembership(clusterId)";
        handle.runSQLupdate(sql);
        sql = "create index probeId_index on clusterMembership(probeId)";
        handle.runSQLupdate(sql);
        handle.closeConnection();
    }

    public void makeClusterDataTable_isgsOnly() {
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql = "drop table if exists clusterData_isgsOnly";
        handle.runSQLupdate(sql);
        sql = "create table clusterData_isgsOnly (id int(10) primary key auto_increment, clusterId int(5) not null, probeId int(12) not null, coefficient double not null, monkeyType char(1) not null, cellType char(1) not null)";
        handle.runSQLupdate(sql);
        sql = "create index clusterId_index on clusterData_isgsOnly(clusterId)";
        handle.runSQLupdate(sql);
        sql = "create index probeId_index on clusterData_isgsOnly(probeId)";
        handle.runSQLupdate(sql);
        handle.closeConnection();
    }

    public void makeClusterMembershipTable_isgsOnly() {
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql = "drop table if exists clusterMembership_isgsOnly";
        handle.runSQLupdate(sql);
        sql = "create table clusterMembership_isgsOnly (id int(10) primary key auto_increment, clusterId int(5) not null, probeId int(12) not null, coefficient double not null, monkeyType char(1) not null, cellType char(1) not null)";
        handle.runSQLupdate(sql);
        sql = "create index clusterId_index on clusterMembership_isgsOnly(clusterId)";
        handle.runSQLupdate(sql);
        sql = "create index probeId_index on clusterMembership_isgsOnly(probeId)";
        handle.runSQLupdate(sql);
        handle.closeConnection();
    }

    private void insert(Handle handle, String tableName, String clusterId, String probeId, double coeff) {
        int pId = Integer.parseInt(probeId.substring(3));
        MonkeyType mt = MonkeyType.valueOf(probeId.substring(0, 1));
        CellType ct = CellType.valueOf(probeId.substring(1, 2));
        String sql = "insert into " + tableName + " values(0," + clusterId + ", " + pId + ", " + coeff + ", '" + mt.name() + "', '" + ct.name() + "')";
        handle.runSQLupdate(sql);
    }

    private void processMembers(Handle h, TreeMap<String, Double> bestCoefficients, TreeMap<String, String> membership) {
        for (String probeId : bestCoefficients.keySet()) {
            double coeff = bestCoefficients.get(probeId);
            String clusterId = membership.get(probeId);
            if(!isgsOnly){
                insert(h, "clusterMembership", clusterId, probeId, coeff);
            } else {
                insert(h, "clusterMembership_isgsOnly", clusterId, probeId, coeff);
            }
        }
    }
}
